package com.pickgliss.ui.controls
{
	import com.pickgliss.ui.core.Disposeable;
	import com.pickgliss.utils.ObjectUtils;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;

	public class SelectedButtonGroup extends EventDispatcher implements Disposeable
	{
		public function SelectedButtonGroup(canUnSelect:Boolean = false,mutiSelectCount:int = 1)
		{
			_mutiSelectCount = mutiSelectCount;
			_canUnSelect = canUnSelect;
			_items = new Vector.<ISelectable>();
		}

		private var _canUnSelect:Boolean;
		private var _currentSelecetdIndex:int = -1;
		private var _items:Vector.<ISelectable>;
		private var _lastSelectedButton:ISelectable;
		private var _mutiSelectCount:int;

		/**
		 * 
		 * @param item 添加一个单元
		 * 
		 */		
		public function addSelectItem(item:ISelectable):void
		{
			item.addEventListener(MouseEvent.CLICK,__onItemClicked);
			item.autoSelect = false;
			_items.push(item);
		}
		public function removeAllItem():void
		{
			for(var i:int = 0; i < _items.length; i ++)
			{
				_items[i].removeEventListener(MouseEvent.CLICK,__onItemClicked);	
				_items[i].selected = false;
			}
			_items.splice(0,_items.length);//splice(i,1);
		}
		public function dispose():void
		{
			for(var i:int = 0;i<_items.length;)
			{
				removeItemByIndex(0);
			}
			_lastSelectedButton = null;
			_items = null;
		}
		/**
		 * 获取Item的Index
		 * @param item
		 * @return 
		 */
		public function getSelectIndexByItem(item:ISelectable):int
		{
			return _items.indexOf(item);
		}
		public function getSelectedItem():ISelectable
		{
			if(selectIndex == -1)return null;
			return _items[selectIndex];
		}
		public function getLastSelectedItem():ISelectable
		{
			return _lastSelectedButton;
		}
		public function get itemLength():int
		{
			return _items.length;
		}
		/**
		 * 
		 * @param index 通过序号移除单元
		 * 
		 */		
		public function removeItemByIndex(index:int):void
		{
			if(index != -1)
			{
				_items[index].removeEventListener(MouseEvent.CLICK,__onItemClicked);
				ObjectUtils.disposeObject(_items[index]);
				_items.splice(index,1);
			}
		}
		/**
		 * 
		 * @param item 需要移除的单元
		 * 
		 */		
		public function removeSelectItem(item:ISelectable):void
		{
			var index:int = _items.indexOf(item);
			removeItemByIndex(index);
		}
		
		public function get selectIndex():int{
			return _items.indexOf(_lastSelectedButton);
		}
		
		public function getIndexByItem(item:ISelectable):int
		{
			return _items.indexOf(item);
		}
		
		public function set selectIndex(index:int):void
		{
			var changed:Boolean = _currentSelecetdIndex != index;
			if(index == -1)
			{
				if(_lastSelectedButton && selectedCount == _mutiSelectCount)
				{
					_lastSelectedButton.selected = false;
				}
				_currentSelecetdIndex = index;
				_lastSelectedButton = null;
			}
			else
			{
				var target:ISelectable = _items[index];
				if(!target.selected)
				{
					if(_lastSelectedButton && selectedCount == _mutiSelectCount)
					{
						_lastSelectedButton.selected = false;
					}
					target.selected = true;
					_currentSelecetdIndex = index;
					_lastSelectedButton = target;
				}
				else
				{
					if(_canUnSelect)target.selected = false
				}
			}
			if(changed)dispatchEvent(new Event(Event.CHANGE));
		}
		/**
		 * 
		 * @return 选中的数量
		 * 
		 */		
		public function get selectedCount():int
		{
			var result:int = 0;
			for(var i:int = 0;i<_items.length;i++)
			{
				if(_items[i].selected)  result ++;
			}
			return result
		}
		
		public function set selectedCount(value:int):void
		{
			_mutiSelectCount = value;
		}
		
		private function __onItemClicked(event:Event):void
		{
			event.stopPropagation();
			var target:ISelectable = event.currentTarget as ISelectable;
			selectIndex = _items.indexOf(target);
		}
	}
}